package com.ymatou.iapi.productimporter.testcase;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.common.collect.Lists;
import com.ymatou.iapi.productimporter.parameter.CreateBean;
import com.ymatou.iapi.productimporter.parameter.ExecuteBean;
import com.ymatou.iapi.productimporter.parameter.TaobaoZhuShouExportBean;
import com.ymatou.iapi.productimporter.parameter.YMTExportBean;
import com.ymatou.iapi.productimporter.service.ExecuteCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductImporterCallService;
import com.ymttest.business.service.ProductManagerCallServiceV3;
import com.ymttest.database.model.ProductImportDetail;
import com.ymttest.database.model.ProductImportTask;
import com.ymttest.database.model.YmtCatalogsI;
import com.ymttest.database.model.YmtProductsI;
import com.ymttest.database.sqlwapper.ProductImporterWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.database.sqlwapper.YmtProductsIWapper;
import com.ymttest.utils.AgentUsers;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class Ts_Execute {
    private static ExecuteBean executeBean;
    private static ExecuteCall executeCall;
    private static int vipRootUserId = AgentUsers.vipRootUserId;


    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("根据文件签名查询任务");
    }

    @Before
    public void caseUp() {
        executeBean=new ExecuteBean();
        executeCall=new ExecuteCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(executeCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Ts_Execute_01() {
        Logger.start(true,"根据文件签名查询任务,0初始创建");
        try {
            String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
            TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.0", 50,picUrl);
            List<TaobaoZhuShouExportBean> datas = Arrays.asList(tb);
            String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
            String upl= TestDataManager.UploadFile(s);
            Logger.debug(upl);

            CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
            createBean.getPacketList().get(0).setPacketUrl(upl);
            int taskId = ProductImporterCallService.create(createBean);
            ProductImporterCallService.execute(taskId,createBean.getAppId());

            Thread.sleep(2000);

            ProductImporterWapper productImporterWapper = new ProductImporterWapper();
            List<ProductImportDetail> productImportDetails = productImporterWapper.selectProductImportDetailByTaskId(taskId);
            YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
            YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
            productImportDetails.forEach(p->{
                try {
                    YmtProductsI ymtProductsI = ymtProductsIWapper.selectProductByProductId(p.getProductId()).get(0);
                    YmtCatalogsI ymtCatalogsI = ymtCatalogsIWapper.selectCatalogsByProductId(p.getProductId()).get(0);
                    Logger.verifyEquals(tb.getTitle(),"\""+ymtProductsI.getsProduct()+"\"","验证Title");
                    Logger.verifyEquals(Double.parseDouble(tb.getPrice()),ymtCatalogsI.getfQuotePrice().doubleValue(),"验证price");
                    Logger.verifyEquals(tb.getNum(),ymtCatalogsI.getiNum(),"验证num");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Ts_Execute_02() {
        Logger.start(true,"根据文件签名查询任务,0初始创建");
        try {
            String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
            List<TaobaoZhuShouExportBean> datas = new ArrayList<>();
            for (int i=0;i<10;i++){
                TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.0", 50,picUrl);
                datas.add(tb);
            }
            String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
            String upl= TestDataManager.UploadFile(s);

            CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
            createBean.getPacketList().get(0).setPacketUrl(upl);
            int taskId = ProductImporterCallService.create(createBean);
            ProductImporterCallService.execute(taskId,null);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Ts_Execute_03() {
        Logger.start(true,"根据文件签名查询任务,文件名超过500个字符");
        try {
            String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
            List<TaobaoZhuShouExportBean> datas = new ArrayList<>();
            String name = "好";
            for (int i=0;i<500;i++){
                name = name+"好";
            }
            TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.0", 50,picUrl);
            TaobaoZhuShouExportBean tb2 = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.0", 50,picUrl);
            TaobaoZhuShouExportBean tb1 = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean(name, "12.0", 50,picUrl);
            datas.add(tb);
            datas.add(tb1);
            datas.add(tb2);
            String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
            String upl= TestDataManager.UploadFile(s);

            CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
            createBean.getPacketList().get(0).setPacketUrl(upl);
            int taskId = ProductImporterCallService.create(createBean);
            ProductImporterCallService.execute(taskId,null);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category({P1.class})
    @TestCase
    public void Ts_Execute_04() {
        Logger.start(true,"根据文件签名查询任务,商品描述图片不能超过30张");
        try {
            String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
            List<TaobaoZhuShouExportBean> datas = new ArrayList<>();

            TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.0", 50,picUrl);
            String Wireless_desc = "\"<wapDesc>";

            String picUrl1 = "<img>http://s2.ymatou.com/home/4aa0780b5ffeee46a01f132052fdb71a.png</img>";
//                +"<img>http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg</img>"
//                +"<img>http://pc1.img.ymatou.com/G01/shangou/M00/2A/17/rBBlD1eqnrWAKbOiAANhqAY9_CQ026_n_w_o.jpg</img>"
//                + "<img>http://pic1.ymatou.com/G01/M00/00/49/rBBk0VXBsxOAVu1hAANB86UXukw722_original.jpg</img>"
//                + "<img>http://pic1.ymatou.com/G01/M00/00/39/rBBk0VXA4giAMR32AAHXshuvqIU081_original.jpg</img>"
//                + "<img>http://pic1.ymatou.com/G01/M00/00/26/rBBk0VXAwVqAW2G2AAGN5f58RIk585_original.jpg</img>"
//                + "<img>http://pic1.ymatou.com/G01/M00/00/26/rBBk0VXAwVWAeU8tAAHJqsVDe2s611_original.jpg</img>"
//                +"<img>http://pic1.ymatou.com/G01/M00/00/49/rBBk0VXBsxKAK6EJAANB86UXukw374_original.jpg</img>"
//                +"<img>http://pic1.ymatou.com/G01/M00/00/55/rBBk0VXCBFWAfBH1AATQUEo-_Go693_original.jpg</img>"
//                +"<img>http://pic1.ymatou.com/G01/M00/00/55/rBBk0VXCBFeAMsCYAAG2-ErDmY4172_original.jpg</img>"
//                +"<img>http://pic1.ymatou.com/G01/M00/00/55/rBBk0VXCBFiAbt7OAAWz7Xzv0fk342_original.jpg</img>"
//                +"<img>http://pic1.ymatou.com/G01/M00/00/57/rBBk0VXCCgmAMrajAAHXshuvqIU297_original.jpg</img>"
//                + "<img>http://pic1.ymatou.com/G01/M00/00/57/rBBk0VXCCgqAcn2UAAG4u1jwmtE279_original.jpg</img>"
//                +"<img>http://pic1.ymatou.com/G01/M00/00/57/rBBk0VXCCg2AJWEuAADXjF_RCuw338_original.jpg</img>"
//                +"<img>http://pic1.ymatou.com/G01/M00/00/57/rBBk0VXCCgyAM4wFAAUeMk33rMI007_original.jpg</img>"
//                +"<img>http://pic1.ymatou.com/G01/M00/00/57/rBBk0VXCCgyAA316AARvh-jwGEU114_original.jpg</img>";
//            String picUrl17= "";
//            String picUrl18= "";
//            String picUrl19= "";
//            String picUrl20= "";
//            String picUrl21= "http://pic1.ymatou.com/G01/M00/00/39/rBBk0VXA4giAMR32AAHXshuvqIU081_original.jpg";
//            String picUrl22= "http://pic1.ymatou.com/G01/M00/00/39/rBBk0VXA4giAMR32AAHXshuvqIU081_original.jpg";
//            String picUrl23= "http://pic1.ymatou.com/G01/M00/00/39/rBBk0VXA4giAMR32AAHXshuvqIU081_original.jpg";
//            String picUrl24= "http://pic1.ymatou.com/G01/M00/00/39/rBBk0VXA4giAMR32AAHXshuvqIU081_original.jpg";
//            String picUrl25= "http://pic1.ymatou.com/G01/M00/00/39/rBBk0VXA4giAMR32AAHXshuvqIU081_original.jpg";
//            String picUrl26= "http://pic1.ymatou.com/G01/M00/00/39/rBBk0VXA4giAMR32AAHXshuvqIU081_original.jpg";
//            String picUrl27= "http://pic1.ymatou.com/G01/M00/00/39/rBBk0VXA4giAMR32AAHXshuvqIU081_original.jpg";
//            String picUrl28= "http://pic1.ymatou.com/G01/M00/00/39/rBBk0VXA4giAMR32AAHXshuvqIU081_original.jpg";
//            String picUrl29= "http://pic1.ymatou.com/G01/M00/00/39/rBBk0VXA4giAMR32AAHXshuvqIU081_original.jpg";
//            String picUrl30= "http://pic1.ymatou.com/G01/M00/00/39/rBBk0VXA4giAMR32AAHXshuvqIU081_original.jpg";
//            String picUrl31= "http://pic1.ymatou.com/G01/M00/00/39/rBBk0VXA4giAMR32AAHXshuvqIU081_original.jpg";

            Wireless_desc = Wireless_desc +picUrl1+ "</wapDesc>\"";
//            parseDetailPicUrls(Wireless_desc);
            tb.setWireless_desc(Wireless_desc);
            datas.add(tb);
            String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
            String upl= TestDataManager.UploadFile(s);

            CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
            createBean.getPacketList().get(0).setPacketUrl(upl);
            int taskId = ProductImporterCallService.create(createBean);
            ProductImporterCallService.execute(taskId,null);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    @Test
    @Category({P1.class})
    @TestCase
    public void Ts_Execute_05() {
        Logger.start(true,"根据文件签名查询任务,0初始创建-YMT导入");
        try {
        	String f1 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "1.jpg";
        	String picUrl= TestDataManager.UploadFile(f1);
        	String f2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "upload.bmp";
        	picUrl+=";"+ TestDataManager.UploadFile(f2);
//            String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
            YMTExportBean ymtb = ProductManagerCallServiceV3.initOneYMTExportBean("YMT商品",picUrl);
            ymtb.setTariff_rate("0.05");
            List<YMTExportBean> datas = Arrays.asList(ymtb);
         // 随机产物名
    		String tname = YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmSS");
    		ProductManagerCallServiceV3.CreateYMTExportFile(tname + ".csv", datas);
    		// tmp\ymd\ --产物目录
    		String tmpfd = "temp" + File.separatorChar + tname + File.separatorChar;
    		TestDataManager.MoveFile(tname + ".csv", tmpfd);
    		String uploadfile=tmpfd+ File.separatorChar+tname + ".csv";
            String upl= TestDataManager.UploadFile(uploadfile);
            Logger.debug("upl:"+upl);

            CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
            createBean.setFileName(tname + ".csv");
            createBean.setProductSource(2);
            createBean.getPacketList().get(0).setPacketUrl(upl);
            int taskId = ProductImporterCallService.create(createBean);
            ProductImporterCallService.execute(taskId,createBean.getAppId());

            Thread.sleep(5000);
//
            ProductImporterWapper productImporterWapper = new ProductImporterWapper();
            List<ProductImportDetail> productImportDetails = productImporterWapper.selectProductImportDetailByTaskId(taskId);
            Logger.debug("productImportDetails:"+FormatUtil.beanToGSONString(productImportDetails));
            verifyYMTDB(productImportDetails,datas);
            YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
            YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
            productImportDetails.forEach(p->{
                try {
                	List<YmtProductsI> list = ymtProductsIWapper.selectProductByProductId(p.getProductId());
                	int redo=0;
                	while(list.size()<=0&&redo<10) {
                		redo++;
                		Thread.sleep(1000);
                		list = ymtProductsIWapper.selectProductByProductId(p.getProductId());
                	}
                    YmtProductsI ymtProductsI =list.get(0);
                    List<YmtCatalogsI> ymtCatalogsI = ymtCatalogsIWapper.selectCatalogsByProductId(p.getProductId());
                    Logger.debug("ymtCatalogsI:"+FormatUtil.beanToGSONString(ymtCatalogsI));
                    Logger.verifyEquals(ymtb.getTitle(),ymtProductsI.getsProduct(),"验证Title");
                    Logger.verifyEquals(Double.parseDouble(ymtb.getPrice()),ymtCatalogsI.get(0).getfQuotePrice().doubleValue(),"验证price");
                    Logger.verifyEquals(ymtb.getStock(),ymtCatalogsI.get(0).getiNum().toString(),"验证num");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            Logger.fail(e);
        }
    }


	@Test
    @Category({P1.class})
    @TestCase
    public void Ts_Execute_06() {
        Logger.start(true,"根据文件签名查询任务,0初始创建-YMT导入-多条记录混合数据");
        try {
        	String f1 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "1.jpg";
        	String picUrl= TestDataManager.UploadFile(f1);
        	String f2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "upload.bmp";
        	picUrl+=";"+ TestDataManager.UploadFile(f2);
//            String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
            YMTExportBean ymtb = ProductManagerCallServiceV3.initYMTExportBean("YMT商品1",picUrl);
            YMTExportBean ymtb2 = ProductManagerCallServiceV3.initYMTExportBean("YMT商品2",picUrl);
            ymtb2.setPrice("1;2;3;4;5;;7;8;9;;;");
            ymtb2.setStock("11;22;33;44;;;;;99;;;;");
            ymtb2.setBrandid(null);
            ymtb2.setSku(null);
            YMTExportBean ymtb3 =new YMTExportBean();
            ymtb3.setTitle("title3");
            ymtb3.setPic(picUrl);
            ymtb3.setProductcode(TestDataManager.getRandomUUID().toString());
            YMTExportBean ymtb4 = ProductManagerCallServiceV3.initYMTExportBean("YMT商品4",picUrl);
            ymtb4.setCatalog_type1_value("红色;蓝色");
            ymtb4.setCatalog_type2_value("M;L");
            ymtb4.setPrice("1;2;3;4");
            ymtb4.setStock("11;12;13;14");
            List<YMTExportBean> datas = Arrays.asList(ymtb,ymtb2,ymtb3,ymtb4);
         // 随机产物名
    		String tname = YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmSS");
    		ProductManagerCallServiceV3.CreateYMTExportFile(tname + ".csv", datas);
    		// tmp\ymd\ --产物目录
    		String tmpfd = "temp" + File.separatorChar + tname + File.separatorChar;
    		TestDataManager.MoveFile(tname + ".csv", tmpfd);
    		String uploadfile=tmpfd+ File.separatorChar+tname + ".csv";
            String upl= TestDataManager.UploadFile(uploadfile);
            Logger.debug("upl:"+upl);

            CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
            createBean.setFileName(tname + ".csv");
            createBean.setProductSource(2);
            createBean.getPacketList().get(0).setPacketUrl(upl);
            int taskId = ProductImporterCallService.create(createBean);
            ProductImporterCallService.execute(taskId,createBean.getAppId());

            Thread.sleep(10000);
//
            ProductImporterWapper productImporterWapper = new ProductImporterWapper();
            List<ProductImportDetail> productImportDetails = productImporterWapper.selectProductImportDetailByTaskId(taskId);
            Logger.debug("productImportDetails:"+FormatUtil.beanToGSONString(productImportDetails));
            verifyYMTDB(productImportDetails,datas);
            YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
            YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
            productImportDetails.forEach(p->{
                try {
                	List<YmtProductsI> list = ymtProductsIWapper.selectProductByProductId(p.getProductId());
                	int redo=0;
                	while(list.size()<=0&&redo<100) {
                		redo++;
                		Thread.sleep(1000);
                		list = ymtProductsIWapper.selectProductByProductId(p.getProductId());
                	}
                	Logger.debug("list:"+FormatUtil.beanToGSONString(list));
                    YmtProductsI ymtProductsI =list.get(0);
                    List<YmtCatalogsI> ymtCatalogsI = ymtCatalogsIWapper.selectCatalogsByProductId(p.getProductId());
                    Logger.debug("ymtCatalogsI:"+FormatUtil.beanToGSONString(ymtCatalogsI));
//                    Logger.verifyEquals(ymtb.getTitle(),ymtProductsI.getsProduct(),"验证Title");
//                    Logger.verifyEquals(Double.parseDouble(ymtb.getPrice()),ymtCatalogsI.get(0).getfQuotePrice().doubleValue(),"验证price");
//                    Logger.verifyEquals(ymtb.getStock(),ymtCatalogsI.get(0).getiNum().toString(),"验证num");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
	@Test
    @Category({P1.class})
    @TestCase
    public void Ts_Execute_07() {
		
        Logger.start(true,"根据文件签名查询任务,0初始创建-YMT导入-没有title失败");
        try {
        	String f1 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "1.jpg";
        	String picUrl= TestDataManager.UploadFile(f1);
        	String f2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "upload.bmp";
        	picUrl+=";"+ TestDataManager.UploadFile(f2);
//            String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
            YMTExportBean ymtb = ProductManagerCallServiceV3.initOneYMTExportBean("YMT商品",picUrl);
            ymtb.setTitle(null);
            List<YMTExportBean> datas = Arrays.asList(ymtb);
         // 随机产物名
    		String tname = YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmSS");
    		ProductManagerCallServiceV3.CreateYMTExportFile(tname + ".csv", datas);
    		// tmp\ymd\ --产物目录
    		String tmpfd = "temp" + File.separatorChar + tname + File.separatorChar;
    		TestDataManager.MoveFile(tname + ".csv", tmpfd);
    		String uploadfile=tmpfd+ File.separatorChar+tname + ".csv";
            String upl= TestDataManager.UploadFile(uploadfile);
            Logger.debug("upl:"+upl);

            CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
            createBean.setFileName(tname + ".csv");
            createBean.setProductSource(2);
            createBean.getPacketList().get(0).setPacketUrl(upl);
            int taskId = ProductImporterCallService.create(createBean);
            ProductImporterCallService.execute(taskId,createBean.getAppId());

            Thread.sleep(2000);
//
            ProductImporterWapper productImporterWapper = new ProductImporterWapper();
            List<ProductImportDetail> productImportDetails = productImporterWapper.selectProductImportDetailByTaskId(taskId);
            Logger.debug("productImportDetails:"+FormatUtil.beanToGSONString(productImportDetails));
            Logger.verifyEquals(1, productImportDetails.size(), "1条记录");
            Logger.verifyEquals(false, productImportDetails.get(0).getIsSuccess(), "验证导入失败");
            Logger.verifyEquals(1, productImportDetails.get(0).getFailReasonType(), "验证导入失败FailReasonType");
            Logger.comment("导入失败原因："+productImportDetails.get(0).getFailReasonDes());
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
	@Test
    @Category({P1.class})
    @TestCase
    public void Ts_Execute_08() {
        Logger.start(true,"根据文件签名查询任务,0初始创建-YMT导入-没有pic失败");
        try {
//        	String f1 = System.getProperty("user.dir") + File.separator
//					+ "resource" + File.separator + "1.jpg";
//        	String picUrl= TestDataManager.UploadFile(f1);
//        	String f2 = System.getProperty("user.dir") + File.separator
//					+ "resource" + File.separator + "upload.bmp";
//        	picUrl+=";"+ TestDataManager.UploadFile(f2);
//            String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
            YMTExportBean ymtb = ProductManagerCallServiceV3.initOneYMTExportBean("YMT商品",null);
//            ymtb.setTitle(null);
            List<YMTExportBean> datas = Arrays.asList(ymtb);
         // 随机产物名
    		String tname = YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmSS");
    		ProductManagerCallServiceV3.CreateYMTExportFile(tname + ".csv", datas);
    		// tmp\ymd\ --产物目录
    		String tmpfd = "temp" + File.separatorChar + tname + File.separatorChar;
    		TestDataManager.MoveFile(tname + ".csv", tmpfd);
    		String uploadfile=tmpfd+ File.separatorChar+tname + ".csv";
            String upl= TestDataManager.UploadFile(uploadfile);
            Logger.debug("upl:"+upl);

            CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
            createBean.setFileName(tname + ".csv");
            createBean.setProductSource(2);
            createBean.getPacketList().get(0).setPacketUrl(upl);
            int taskId = ProductImporterCallService.create(createBean);
            ProductImporterCallService.execute(taskId,createBean.getAppId());

            Thread.sleep(2000);
//
            ProductImporterWapper productImporterWapper = new ProductImporterWapper();
            List<ProductImportDetail> productImportDetails = productImporterWapper.selectProductImportDetailByTaskId(taskId);
            Logger.debug("productImportDetails:"+FormatUtil.beanToGSONString(productImportDetails));
            Logger.verifyEquals(1, productImportDetails.size(), "1条记录");
            Logger.verifyEquals(false, productImportDetails.get(0).getIsSuccess(), "验证导入失败");
            Logger.verifyEquals(2, productImportDetails.get(0).getFailReasonType(), "验证导入失败FailReasonType");
            Logger.comment("导入失败原因："+productImportDetails.get(0).getFailReasonDes());
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
	@Test
    @Category({P1.class})
    @TestCase
    public void Ts_Execute_09() {
        Logger.start(true,"根据文件签名查询任务,0初始创建-YMT导入-没有Productcode失败");
        try {
        	String f1 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "1.jpg";
        	String picUrl= TestDataManager.UploadFile(f1);
        	String f2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "upload.bmp";
        	picUrl+=";"+ TestDataManager.UploadFile(f2);
//            String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
            YMTExportBean ymtb = ProductManagerCallServiceV3.initOneYMTExportBean("YMT商品",picUrl);
            ymtb.setProductcode(null);
            List<YMTExportBean> datas = Arrays.asList(ymtb);
         // 随机产物名
    		String tname = YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmSS");
    		ProductManagerCallServiceV3.CreateYMTExportFile(tname + ".csv", datas);
    		// tmp\ymd\ --产物目录
    		String tmpfd = "temp" + File.separatorChar + tname + File.separatorChar;
    		TestDataManager.MoveFile(tname + ".csv", tmpfd);
    		String uploadfile=tmpfd+ File.separatorChar+tname + ".csv";
            String upl= TestDataManager.UploadFile(uploadfile);
            Logger.debug("upl:"+upl);

            CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
            createBean.setFileName(tname + ".csv");
            createBean.setProductSource(2);
            createBean.getPacketList().get(0).setPacketUrl(upl);
            int taskId = ProductImporterCallService.create(createBean);
            ProductImporterCallService.execute(taskId,createBean.getAppId());

            Thread.sleep(2000);
//
            ProductImporterWapper productImporterWapper = new ProductImporterWapper();
            List<ProductImportDetail> productImportDetails = productImporterWapper.selectProductImportDetailByTaskId(taskId);
            Logger.debug("productImportDetails:"+FormatUtil.beanToGSONString(productImportDetails));
            Logger.verifyEquals(1, productImportDetails.size(), "1条记录");
            Logger.verifyEquals(false, productImportDetails.get(0).getIsSuccess(), "验证导入失败");
            Logger.verifyEquals(5, productImportDetails.get(0).getFailReasonType(), "验证导入失败FailReasonType");
            Logger.comment("导入失败原因："+productImportDetails.get(0).getFailReasonDes());
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
	@Test
    @Category({P1.class})
    @TestCase
    @Ignore
    public void Ts_Execute_10() {
        Logger.start(true,"根据文件签名查询任务,0初始创建-YMT导入-大量文件");
        try {
        	String f1 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "1.jpg";
        	String picUrl= TestDataManager.UploadFile(f1);
        	String f2 = System.getProperty("user.dir") + File.separator
					+ "resource" + File.separator + "upload.bmp";
        	picUrl+=";"+ TestDataManager.UploadFile(f2);
        	 List<YMTExportBean> datas = new ArrayList<YMTExportBean>();
//            String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
        	 for (int i = 0; i < 500; i++) {
        		 YMTExportBean ymtb = ProductManagerCallServiceV3.initOneYMTExportBean("YMT商品",picUrl);
                 ymtb.setTariff_rate("0.05");
                 datas.add(ymtb);
			}
           
         // 随机产物名
    		String tname = YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmSS");
    		ProductManagerCallServiceV3.CreateYMTExportFile(tname + ".csv", datas);
    		// tmp\ymd\ --产物目录
    		String tmpfd = "temp" + File.separatorChar + tname + File.separatorChar;
    		TestDataManager.MoveFile(tname + ".csv", tmpfd);
    		String uploadfile=tmpfd+ File.separatorChar+tname + ".csv";
            String upl= TestDataManager.UploadFile(uploadfile);
            Logger.debug("upl:"+upl);
            //1---1528855588            500146002			SIT业务测试账号
            CreateBean createBean = ProductImporterCallService.initCreateBean(500146002);
            createBean.setFileName(tname + ".csv");
            createBean.setProductSource(2);
            createBean.getPacketList().get(0).setPacketUrl(upl);
            int taskId = ProductImporterCallService.create(createBean);
            ProductImporterCallService.execute(taskId,createBean.getAppId());

//            Thread.sleep(2000);
////
//            ProductImporterWapper productImporterWapper = new ProductImporterWapper();
//            List<ProductImportDetail> productImportDetails = productImporterWapper.selectProductImportDetailByTaskId(taskId);
//            Logger.debug("productImportDetails:"+FormatUtil.beanToGSONString(productImportDetails));
//            verifyYMTDB(productImportDetails,datas);
//            YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
//            YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
//            productImportDetails.forEach(p->{
//                try {
//                	List<YmtProductsI> list = ymtProductsIWapper.selectProductByProductId(p.getProductId());
//                	int redo=0;
//                	while(list.size()<=0&&redo<5) {
//                		redo++;
//                		Thread.sleep(1000);
//                		list = ymtProductsIWapper.selectProductByProductId(p.getProductId());
//                	}
//                    YmtProductsI ymtProductsI =list.get(0);
//                    List<YmtCatalogsI> ymtCatalogsI = ymtCatalogsIWapper.selectCatalogsByProductId(p.getProductId());
//                    Logger.debug("ymtCatalogsI:"+FormatUtil.beanToGSONString(ymtCatalogsI));
//                    Logger.verifyEquals(ymtb.getTitle(),ymtProductsI.getsProduct(),"验证Title");
//                    Logger.verifyEquals(Double.parseDouble(ymtb.getPrice()),ymtCatalogsI.get(0).getfQuotePrice().doubleValue(),"验证price");
//                    Logger.verifyEquals(ymtb.getStock(),ymtCatalogsI.get(0).getiNum().toString(),"验证num");
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            });
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
//    public static void main(String[] args) throws IOException {
//        ExecuteBean executeBean = new ExecuteBean();
//        executeBean.setTaskId(167);
//        executeBean.setAppId(TestDataManager.getRandomNum(6));
//        Logger.debug(executeBean);
//        ExecuteCall executeCall = new ExecuteCall();
//        executeCall.setData(executeBean);
//        executeCall.callService();
//    }
    
//    private Map CreateYMTExportFile(YMTExportBean ymtb) throws Exception{
////		String f1 = System.getProperty("user.dir") + File.separator
////				+ "resource" + File.separator + "1.jpg";
////    	String picUrl= TestDataManager.UploadFile(f1);
////    	String f2 = System.getProperty("user.dir") + File.separator
////				+ "resource" + File.separator + "upload.bmp";
////    	picUrl+=";"+ TestDataManager.UploadFile(f2);
//        List<YMTExportBean> datas = Arrays.asList(ymtb);
//     // 随机产物名
//		String tname = YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmSS");
//		ProductManagerCallServiceV3.CreateYMTExportFile(tname + ".csv", datas);
//		// tmp\ymd\ --产物目录
//		String tmpfd = "temp" + File.separatorChar + tname + File.separatorChar;
//		TestDataManager.MoveFile(tname + ".csv", tmpfd);
//		String uploadfile=tmpfd+ File.separatorChar+tname + ".csv";
//        String upl= TestDataManager.UploadFile(uploadfile);
//        TestDataManager.getFileMD5(uploadfile);
//        Map m=new HashMap<String, Object>();
//        m.put("file", upl);
//        m.put("param", datas);
//        m.put("md5", TestDataManager.getFileMD5(uploadfile));
//        return m;
//	}
    @Test
    @Category({P1.class})
    @TestCase
    public void Ts_Execute_11() {
        Logger.start(true,"根据文件签名查询任务,0初始创建-没有文描也能成功-JIRACONN-33320");
        try {
            String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
            TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.0", 50,picUrl);
            tb.setDescription(null);
            List<TaobaoZhuShouExportBean> datas = Arrays.asList(tb);
            String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
            String upl= TestDataManager.UploadFile(s);
            Logger.debug(upl);

            CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
            createBean.getPacketList().get(0).setPacketUrl(upl);
            int taskId = ProductImporterCallService.create(createBean);
            ProductImporterCallService.execute(taskId,createBean.getAppId());

            Thread.sleep(2000);

            ProductImporterWapper productImporterWapper = new ProductImporterWapper();
            List<ProductImportDetail> productImportDetails = productImporterWapper.selectProductImportDetailByTaskId(taskId);
            YmtProductsIWapper ymtProductsIWapper = new YmtProductsIWapper();
            YmtCatalogsIWapper ymtCatalogsIWapper = new YmtCatalogsIWapper();
            productImportDetails.forEach(p->{
                try {
                    YmtProductsI ymtProductsI = ymtProductsIWapper.selectProductByProductId(p.getProductId()).get(0);
                    YmtCatalogsI ymtCatalogsI = ymtCatalogsIWapper.selectCatalogsByProductId(p.getProductId()).get(0);
                    Logger.verifyEquals(tb.getTitle(),"\""+ymtProductsI.getsProduct()+"\"","验证Title");
                    Logger.verifyEquals(Double.parseDouble(tb.getPrice()),ymtCatalogsI.getfQuotePrice().doubleValue(),"验证price");
                    Logger.verifyEquals(tb.getNum(),ymtCatalogsI.getiNum(),"验证num");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    @Test
    @Category({P1.class})
    @TestCase
    public void Ts_Execute_12() {
        Logger.start(true,"根据文件签名查询任务,0初始创建-YMT导入-图片异常失败");
        try {
//        	String f1 = System.getProperty("user.dir") + File.separator
//					+ "resource" + File.separator + "1.jpg";
        	String picUrl="http://www.ymatou.com/1.jpg";
//        	String f2 = System.getProperty("user.dir") + File.separator
//					+ "resource" + File.separator + "upload.bmp";
//        	picUrl+=";"+ TestDataManager.UploadFile(f2);
//            String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
            YMTExportBean ymtb = ProductManagerCallServiceV3.initOneYMTExportBean("YMT商品",picUrl);
            ymtb.setTariff_rate("0.05");
            List<YMTExportBean> datas = Arrays.asList(ymtb);
         // 随机产物名
    		String tname = YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmSS");
    		ProductManagerCallServiceV3.CreateYMTExportFile(tname + ".csv", datas);
    		// tmp\ymd\ --产物目录
    		String tmpfd = "temp" + File.separatorChar + tname + File.separatorChar;
    		TestDataManager.MoveFile(tname + ".csv", tmpfd);
    		String uploadfile=tmpfd+ File.separatorChar+tname + ".csv";
            String upl= TestDataManager.UploadFile(uploadfile);
            Logger.debug("upl:"+upl);

            CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
            createBean.setFileName(tname + ".csv");
            createBean.setProductSource(2);
            createBean.getPacketList().get(0).setPacketUrl(upl);
            int taskId = ProductImporterCallService.create(createBean);
            ProductImporterCallService.execute(taskId,createBean.getAppId());

            Thread.sleep(2000);
//
            ProductImporterWapper productImporterWapper = new ProductImporterWapper();
            List<ProductImportDetail> productImportDetails = productImporterWapper.selectProductImportDetailByTaskId(taskId);
            Logger.debug("productImportDetails:"+FormatUtil.beanToGSONString(productImportDetails));
            Logger.verifyEquals(96, productImportDetails.get(0).getFailReasonType(), "验证FailReasonType");
            Logger.verifyEquals("所有图片上传失败", productImportDetails.get(0).getFailReasonDes(), "验证failReasonDes");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    @Test
    @Category({P1.class})
    @TestCase
    public void Ts_Execute_13() {
        Logger.start(true,"根据文件签名查询任务,0初始创建-YMT-没有具体内容失败");
        try {
//        	String f1 = System.getProperty("user.dir") + File.separator
//					+ "resource" + File.separator + "1.jpg";
//        	String picUrl= TestDataManager.UploadFile(f1);
//        	String f2 = System.getProperty("user.dir") + File.separator
//					+ "resource" + File.separator + "upload.bmp";
//        	picUrl+=";"+ TestDataManager.UploadFile(f2);
////            String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
            List<YMTExportBean> datas = new ArrayList<YMTExportBean>();
         // 随机产物名
    		String tname = YMTDateUtil.getBeforeOrNextDay(0, "yyyyMMddHHmmSS");
    		ProductManagerCallServiceV3.CreateYMTExportFile(tname + ".csv", datas);
    		// tmp\ymd\ --产物目录
    		String tmpfd = "temp" + File.separatorChar + tname + File.separatorChar;
    		TestDataManager.MoveFile(tname + ".csv", tmpfd);
    		String uploadfile=tmpfd+ File.separatorChar+tname + ".csv";
            String upl= TestDataManager.UploadFile(uploadfile);
            Logger.debug("upl:"+upl);

            CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
            createBean.setFileName(tname + ".csv");
            createBean.setProductSource(2);
            createBean.getPacketList().get(0).setPacketUrl(upl);
            int taskId = ProductImporterCallService.create(createBean);
            ProductImporterCallService.execute(taskId,createBean.getAppId());

            Thread.sleep(2000);
//
            ProductImporterWapper productImporterWapper = new ProductImporterWapper();
            ProductImportTask productImport = productImporterWapper.selectProductImportTaskByTaskId(taskId);
            Logger.debug("productImport:"+FormatUtil.beanToGSONString(productImport));
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    @Test
    @Category({P1.class})
    @TestCase
    public void Ts_Execute_14() {
        Logger.start(true,"根据文件签名查询任务,0初始创建-TaoBao-没有具体内容失败");
        try {
//            String picUrl = "http://pc1.img.ymatou.com/G01/shangou/M00/2C/D0/rBBlD1gHOV6AY32AAAEN6UG5TOc701_400_401_n_w_l.jpg";
//            TaobaoZhuShouExportBean tb = ProductManagerCallServiceV3.initTaobaoZhuShouExportBean("淘宝商品", "12.0", 50,picUrl);
            List<TaobaoZhuShouExportBean> datas =new ArrayList<TaobaoZhuShouExportBean>();
            String s = "temp"+ File.separator + ProductManagerCallServiceV3.CreateTaoBaoExportZipFile(datas)+".zip";
            String upl= TestDataManager.UploadFile(s);
//            Logger.debug(upl);

            CreateBean createBean = ProductImporterCallService.initCreateBean(vipRootUserId);
            createBean.getPacketList().get(0).setPacketUrl(upl);
            int taskId = ProductImporterCallService.create(createBean);
            ProductImporterCallService.execute(taskId,createBean.getAppId());
            Thread.sleep(3000);
            ProductImporterWapper productImporterWapper = new ProductImporterWapper();
            ProductImportTask productImport = productImporterWapper.selectProductImportTaskByTaskId(taskId);
            Logger.debug("productImport:"+FormatUtil.beanToGSONString(productImport));
            Logger.verifyEquals(4, productImport.getTaskStatus(), "验证TaskStatus");
            Logger.verifyEquals("未找到商品数据文件", productImport.getTaskStatusMsg(), "验证TaskStatusMsg");
        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    @Test
    @Category({P1.class})
    @TestCase
    @Ignore
    public void Test() {
        Logger.start(true,"上传文件");
        try {

    		String uploadfile="p2.csv";
            String upl= TestDataManager.UploadFile(uploadfile);
            Logger.debug("upl:"+upl);

        } catch (Exception e) {
            Logger.fail(e);
        }
    }
    /**
     * 验证数据库
     * @param productImportDetails
     * @param datas
     */
    private void verifyYMTDB( List<ProductImportDetail> productImportDetails,List<YMTExportBean> datas) {
   	 productImportDetails.forEach(detail->{
        	YMTExportBean ymteb = datas.stream().filter(d->{return d.getProductcode().equals(detail.getSourceProductCode());}).findFirst().get();
//        	"sellerId": AgentUsers.vipRootUserId,
        	Logger.verifyEquals(vipRootUserId, detail.getSellerId(), "验证sellerid");
//    		"sourceProductTitle": "YMT商品1",
        	Logger.verifyEquals(ymteb.getTitle(), detail.getSourceProductTitle(), "验证sourceProductTitle");
//    		"sourceProductCode": "7b68976f-c2c8-49a9-a400-30ba9a1c2ab6",
//    		"isSuccess": false,
//    		"failReasonType": 0,
//    		"productDesc": "商品详情文描",
        	Logger.verifyEquals(ymteb.getDesc()==null?"":ymteb.getDesc(), detail.getProductDesc(), "验证productDesc");
//    		"catalogName1": "颜色",
        	Logger.verifyEquals(ymteb.getCatalog_type1()==null?"":ymteb.getCatalog_type1(), detail.getCatalogName1(), "验证catalogName1");
//    		"catalogName1Values": "红色;蓝色;橙色;",
        	Logger.verifyEquals(ymteb.getCatalog_type1_value()==null?"":ymteb.getCatalog_type1_value(), detail.getCatalogName1Values(), "验证catalogName1Values");
//    		"catalogName2": "尺码",
        	Logger.verifyEquals(ymteb.getCatalog_type2()==null?"":ymteb.getCatalog_type2(), detail.getCatalogName2(), "验证catalogName2");
//    		"catalogName2Values": "S;M;L;XL",
        	Logger.verifyEquals(ymteb.getCatalog_type2_value()==null?"":ymteb.getCatalog_type2_value(), detail.getCatalogName2Values(), "验证catalogName2Values");
//    		"prices": "1;2;3;4;5;6;7;8;9;10;11;12",
        	Logger.verifyEquals(ymteb.getPrice()==null?"":ymteb.getPrice(), detail.getPrices(), "验证Prices");
//    		"stocks": "11;12;13;14;15;16;17;18;19;110;111;112",
        	Logger.verifyEquals(ymteb.getStock()==null?"":ymteb.getStock(), detail.getStocks(), "验证stocks");
//    		"skus": "sku1;sku2",
        	Logger.verifyEquals(ymteb.getSku()==null?"":ymteb.getSku(), detail.getSkus(), "验证skus");
//    		"brandId": "AddAgentProductCallService.BrandId",
        	Logger.verifyEquals(ymteb.getBrandid()==null?"":ymteb.getBrandid(), detail.getBrandId(), "验证BrandId");
//    		"thirdCategoryId": "1003",
        	Logger.verifyEquals(ymteb.getThird_categoryid()==null?"":ymteb.getThird_categoryid(), detail.getThirdCategoryId(), "验证thirdCategoryId");
//    		"logisticsModeId": "1",
        	Logger.verifyEquals(ymteb.getLogistics_modeid()==null?"":ymteb.getLogistics_modeid(), detail.getLogisticsModeId(), "验证logisticsModeId");
//    		"idCardForm": "1",
        	Logger.verifyEquals(ymteb.getIdcard_form()==null?"":ymteb.getIdcard_form(), detail.getIdCardForm(), "验证idCardForm");
        	Logger.verifyEquals(ymteb.getTariff_rate()==null?"":ymteb.getTariff_rate(), detail.getTariffRate(), "验证Tariff_rate");
        });
		
	}
    public static List<String> parseDetailPicUrls(String wirelessDesc) {
        List<String> pics = Lists.newArrayList();
        if (StringUtils.isBlank(wirelessDesc)) {
            return pics;
        }

        Pattern pattern = Pattern.compile("<img>(.+?)</img>", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(wirelessDesc);
        while (matcher.find()) {
            String picUrl = StringUtils.trimToEmpty(matcher.group(1));
            if (StringUtils.isBlank(picUrl)) {
                continue;
            }
            if (pics.contains(picUrl)) {
                continue;
            }
            pics.add(picUrl);
        }
        if (pics.size() > 30) {
            return pics.subList(0, 30);
        } else {
            return pics;
        }
    }
}
